import {EXIT, LOGIN_USER, FLASH_USER, FLASH_EXAM, GETUSERDATA, MESSAGENUM, FLASH_MOB_STATUS, BINDMOBILE} from './mutations-types'
import {getCookie, clearCookie, removeSession, setCookie} from '@/config/mUntils'
import {setToken} from '@/service/get-data'
export default{
  [EXIT] (state) {
    clearCookie('Token') // token值
    clearCookie('Exam')
    removeSession('selectedProList') // 退出后 确保本地选择商品被删除
    clearCookie('getTokenTime')
    clearCookie('username')
    clearCookie('USERID')
    clearCookie('mobile')
    clearCookie('needUpdateStatus')
    clearCookie('cookieInfo')
    setToken(true)
    state.isLogin = false // 登录状态
    state.isExam = '' // 认证状态
    state.username = '' // 用户名
    state.mobile = '' // 用户名
    state.level = -1
    state.id = null // id
  },
  [LOGIN_USER] (state, obj) {
    let token = getCookie('Token')
    let username = getCookie('username')
    let id = getCookie('USERID')
    state.isExam = obj.status
    state.mobile = obj.mobile
    state.messageNum = obj.messageNumber
    if (token) {
      state.isLogin = true
      state.username = username
      state.id = id
    } else {
      state.isLogin = false
    }
  },
  [FLASH_USER] (state) {
    let token = getCookie('Token')
    let username = getCookie('username')
    let id = getCookie('USERID')
    if (token) {
      state.isLogin = true
      state.username = username
      state.id = id
    } else {
      state.isLogin = false
    }
  },
  [FLASH_EXAM] (state, status) {
    if (status) {
      state.isExam = status
    }
  },
  [FLASH_MOB_STATUS] (state, obj) {
    state.mobile = obj.mobile
  },
  [GETUSERDATA] (state, info) {
    // debugger
    if (info.data && info.data.resultCode === 2000) {
      let data = info.data.data
      state.username = data.username
      state.id = data.id
      state.isExam = data.status
      state.discount = data.discount
      state.isLogin = true
      state.mobile = data.mobile
      state.messageNum = data.messageNumber
      state.headImg = data.headImg
      state.acceptanceAmount = data.acceptanceAmount || 0
      state.balanceAmount = data.balanceAmount || 0
      setCookie('Exam', data.status)
      setCookie('USERID', data.id)
      state.level = data.level
    } else if (info.data.resultCode === 3001) {
      state.username = ''
      state.isExam = ''
      state.level = -1
      state.mobile = ''
      state.headImg = ''
      state.acceptanceAmount = ''
      state.balanceAmount = ''
      clearCookie('Token')
      clearCookie('Exam')
    }
  },
  [MESSAGENUM] (state, num) {
    if (num) {
      state.messageNum = num
    }
  },
  [BINDMOBILE] (state, status) {
    state.bindMobilePop = status
  }
}
